﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MISAPP.Classify.DataAccess;
using System.Data;

namespace MISAPP.Classify.Service
{
    public class Service_Bonus:Service_Matrix
    {
        /// <summary>
        /// BONUS:年獎員額及盈餘空間
        /// BUDGET:預算執行分析
        /// SQUARED:矩陣表
        /// Q1:季追蹤
        /// COST:營業費用BG 
        /// </summary>
        public enum BonusType
        {
            BONUS,BUDGET, SQUARED, Q1, COST
        };


        protected DA_Bonus da = null;
        protected DA_Bonus DA
        {
            get
            {
                if (da == null)
                {
                    da = new DA_Bonus();
                }
                return da;
            }
        }
        
        public Service_Bonus()
        { }

        public Service_Bonus(string p_team)
        {
            team = p_team;
        }

        public string GetTeam()
        {
            return team;
        }

        public bool Insert(List<Squared> p_squared,string p_user)
        {
            bool ok = false;
            try
            {
                foreach (Squared sq in p_squared)
                {
                    if (false == DA.Insert(sq.MISGroup, sq.Amount
                        , p_user, sq.RowPos, sq.Sheet))
                    {
                        throw new Exception("新增失敗");
                        ok = false;
                    }
                }
                ok = true;
            }
            catch (Exception ex)
            {
                throw new Exception("新增失敗");
                ok = false;
            }
            return ok;
        }

        public DataTable GetDataBySheet(string p_sheet)
        {
            return DA.GetDataBySheet(p_sheet);
        }

        public DataTable GetDataBySheet(string[] p_sheet)
        {
            return DA.GetDataBySheet(p_sheet);
        }

        public List<Squared> GetBonusCell(string p_level)
        {
            return GetParameterValue(DA, new object[] { p_level,team });
        }

        public bool InsertFromXls(string p_xls, string p_level, string p_id, BonusType p_team)
        {
            return DA.InsertFromXls(p_xls, p_level, p_id ,  p_team.ToString());
        }

        /// <summary>
        /// 把資料吐回去到excel中
        /// </summary>
        /// <param name="p_xls"></param>
        /// <param name="p_type"></param>
        public void FillBonus2Xls(string p_xls,BonusType p_type)
        {
           DA.FillBonusData(p_xls, Utility.GetSheets(p_type.ToString()));
        }
    }
}
